
  <!DOCTYPE html>
  <html lang="en">

  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <div id="app"></div>
    <br />
    <div id="app1"></div>
    <style>
      .list {
        height: 200px;
        width: 200px;
        overflow: auto;
      }

      .item {
        height: 40px;
        width: 100%;
      }
    </style>
    <script src="../js/babel.min.js"></script>
    <script src="../js-new/react.development.js"></script>
    <script src="../js-new/react-dom.development.js"></script>
    <script type="text/babel">
    class Demo extends React.Component {
      state = { arr: [] }
    
      componentDidMount() {
        this.timer = setInterval(() => {
          let { arr } = this.state;
          const news = '消息' + (arr.length + 1)
    
          this.setState({ arr: [...arr, news] });
        }, 1000);
      }
    
      /**
      * @description 组件更新
      * @param preProps 更改之前的props
      * @param preState 更改之前的state
      * @param snapshot getSnapshotBeforeUpdate钩子返回的快照值
      */
      componentDidUpdate(preProps, preState, snapshot) {
        // console.log('preProps = ', preProps)
        // console.log('preState = ', preState)
        this.refs.list.scrollTop += this.refs.list.scrollHeight - snapshot
        console.log("snapshot = ", snapshot)
        console.log("demo componentDidUpdate");
      }
    
      /**
      * @description 更新之前获取快照
      */
      getSnapshotBeforeUpdate() {
        console.log("demo getSnapshotBeforeUpdate")
        // return null
        return this.refs.list.scrollHeight
      }
      render() {
        console.log("demo render");
        return (
          <div ref="list" className="list">
            {this.state.arr.map((news, index) => {
              return <div key={index} className="item">{news}</div>
            })}
          </div>
        );
      }
    }
    ReactDOM.render(<Demo />, document.getElementById("app"));
    </script>
  </body>

  </html>
